<?php

/**
 * 文件名： channel.mod.php
 * 作  者：　狐狸 <foxis@qq.com>
 * @version $Id: channel.mod.php 2112443476 2013-05-21 6303 foxis@qq.com $
 * 功能描述： api for JishiGou
 * 版权所有： Powered by JishiGou API 1.0.0 (a) 2005 - 2099 Cenwor Inc.
 * 公司网站： http://cenwor.com
 * 产品网站： http://jishigou.net
 */


if(!defined('IN_JISHIGOU'))
{
    exit('invalid request');
}

class ModuleObject extends MasterObject
{
    
	function ModuleObject($config)
	{
		$this->MasterObject($config);
		$this->ChannelLogic = jlogic('channel');
        $this->Execute();
	}

	function Execute()
	{
		switch($this->Code)
		{
			case 'channel':
                {
                    $this->channel();
                    break;
                }
			case 'topic':
                {
                    $this->topic();
                    break;
                }
			case 'followtopic':
                {
                    $this->followtopic();
                    break;
                }
			case 'followadd':
                {
                    $this->followadd();
                    break;
                }
			case 'followdel':
                {
                    $this->followdel();
                    break;
                }
			case 'isfollow':
				{
					$this->isfollow();
					break;
				}
			default :
    			{
    				$this->Main();
    				break;
    			}
		}
	}

	function Main()
	{
		api_output('channel api is ok');
	}

	
	function channel()
	{
		$uid = max(0, (int) $this->Inputs['uid'] ? $this->Inputs['uid'] : $this->user['uid']);
		$type = in_array($this->Inputs['type'],array('first','second','channels','mychannel')) ? $this->Inputs['type'] : '';		$return = ConfigHandler::get('channel');
		$return['mychannel'] = $this->ChannelLogic->mychannel($uid);
		foreach($return as $key => $val){
			if('channels' != $key){
				$return[$key] = is_array($val) ? array_merge($val) : array();
			}
		}
		if($type){
			$return = $return[$type];
		}
		api_output($return);
	}

	
	function isfollow()
	{
		$ch_id = max(0, (int) $this->Inputs['id']);		$uid = max(0, (int) $this->Inputs['uid'] ? $this->Inputs['uid'] : $this->user['uid']);
		if($ch_id > 0){
			$mychannels = $this->ChannelLogic->mychannel($uid);
			$ch_ids = array_keys($mychannels);
			$return = in_array($ch_id,$ch_ids) ? 1 : 0;
			api_output($return);
		}else{
			api_error('channel id is empty', 10);
		}
	}

	
	function topic()
	{
		$orderby = in_array($this->Inputs['orderby'],array('new','dig','rec')) ? $this->Inputs['orderby'] : 'new';		if($orderby == 'dig'){
			$order = '`lastdigtime` desc';
		}else{
			$order = '`dateline` desc';
		}
		$id = jget('id','int','G');		$channels = ConfigHandler::get('channel');
		$ids = $channels['channels'][$id] ? $channels['channels'][$id] : (strlen($this->Inputs['id']) == 0 && $channels['channels'] ? array() : array(0));
		if($orderby == 'rec'){
			$sql_wheres = array("type"=>"`type`='first'");
			$tids = array(0);
			if($ids){
				$query = DB::query("SELECT tid FROM ".DB::table('topic_recommend')." WHERE item='channel' AND item_id IN(".jimplode($ids).")");
			}else{
				$query = DB::query("SELECT tid FROM ".DB::table('topic_recommend')." WHERE item='channel'");
			}
			while (false != ($row = DB::fetch($query))) {
				$tids[] = $row['tid'];
			}
			$sql_wheres['tid'] = "`tid` IN(".jimplode($tids).")";
		}else{
			$sql_wheres = array("type"=>"`type`='first'","item"=>"`item`='channel'");
			if($ids){
				$sql_wheres['item_id'] = "`item_id` IN(".jimplode($ids).")";
			}
		}
		$this->_topic_list('new',$sql_wheres,$order);
	}

	
	function followtopic()
	{
		$uid = max(0, (int) ($this->Inputs['uid'] ? $this->Inputs['uid'] : $this->user['uid']));
		$orderby = in_array($this->Inputs['orderby'],array('new','dig','rec')) ? $this->Inputs['orderby'] : 'new';		if($orderby == 'dig'){
			$order = '`lastdigtime` desc';
		}else{
			$order = '`dateline` desc';
		}

		$my_buddy_channel = $this->ChannelLogic->mychannel($uid);
		$channels = ConfigHandler::get('channel');
		$channel_channels = $channels['channels'];
		$channel_ids = array();
		if(empty($my_buddy_channel)){
			$channel_ids = array(0);
		}else{
			$my_chs = array_keys($my_buddy_channel);
			foreach($my_chs as $val){
				$channel_ids = array_merge($channel_ids,$channel_channels[$val]);
			}
			$channel_ids = array_unique($channel_ids);
						jtable('members')->clean_new_remind('channel_new', $uid);
		}
		if($orderby == 'rec'){
			$sql_wheres = array("type"=>"`type`='first'");
			$tids = array(0);
			if($channel_ids){
				$query = DB::query("SELECT tid FROM ".DB::table('topic_recommend')." WHERE item='channel' AND item_id IN(".jimplode($channel_ids).")");
			}else{
				$query = DB::query("SELECT tid FROM ".DB::table('topic_recommend')." WHERE item='channel'");
			}
			while (false != ($row = DB::fetch($query))) {
				$tids[] = $row['tid'];
			}
			$sql_wheres['tid'] = "`tid` IN(".jimplode($tids).")";
		}else{
			$sql_wheres = array("type"=>"`type`='first'","item"=>"`item`='channel'");
			if($channel_ids){
				$sql_wheres['item_id'] = "`item_id` IN(".jimplode($channel_ids).")";
			}
		}
		$this->_topic_list('followtopic',$sql_wheres,$order);
	}

	
	function followadd()
	{
		$id = max(0, (int) $this->Inputs['id']);		DB::query("INSERT INTO ".DB::table('buddy_channel')." (`uid`,`ch_id`) SELECT '".MEMBER_ID."','{$id}' FROM ".DB::table('buddy_channel')." WHERE NOT EXISTS(SELECT * FROM ".DB::table('buddy_channel')." WHERE uid='".MEMBER_ID."' AND ch_id='{$id}') LIMIT 1");
		api_output('followadd is ok');
	}

	
	function followdel()
	{
		$id = max(0, (int) $this->Inputs['id']);		DB::query("DELETE FROM ".DB::table('buddy_channel')." WHERE uid = '".MEMBER_ID."' AND ch_id = '$id'");
		api_output('followdel is ok');
	}

}
?>